package com.mcptt.db.loader.msg;

import java.util.List;
import java.util.Vector;

import com.cloudptt.api.product.log.Log;
import com.cloudptt.api.product.operator.AddrOperator;
import com.mcptt.db.DBProvider;
import com.mcptt.db.DbConst;
import com.mcptt.db.model.GroupInfo;
import com.mcptt.db.model.UserInfo;

import android.app.LoaderManager;
import android.content.ContentValues;
import android.content.Context;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.os.Bundle;

public class LoginStateListLoader  implements LoaderManager.LoaderCallbacks<Cursor>{

	private static final String TAG = "LoginStateListLoader";
	private Context ctx = null;
	private AddrOperator op = null;
	private LoginStateListLoaderCb callback = null;
	
	public LoginStateListLoader(Context ctx,AddrOperator op,LoginStateListLoaderCb callback) {
		// TODO Auto-generated constructor stub
		this.ctx = ctx;
		this.op = op;
		this.callback = callback;
	}
	
	@Override
	public Loader<Cursor> onCreateLoader(int id, Bundle args) {
		// TODO Auto-generated method stub
		Log.d(TAG, "onCreateLoader");
//		List<GroupInfo> list = op.getGroupList();
//		for(Iterator<GroupInfo> itor = list.iterator();itor.hasNext();)
		
		return new CursorLoader(this.ctx, DbConst.GROUP_ADDRESS_URI, DBProvider.GROUP_ADDRESS_FULL_PROJECTION, null, null, null);
	}

	@Override
	public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
		// TODO Auto-generated method stub

		if (data == null
				|| data.getCount() < 0)
		{
			return;
		}
		List<GroupInfo> groupList = new Vector<GroupInfo>();
		Cursor cursor = data;
		while (cursor.moveToNext()) 
		{  
			Log.d(TAG, "onLoadFinished next");
			GroupInfo info = new GroupInfo(cursor);
			Log.d(TAG, "onLoadFinished info = " + info);
			
			String groupMcId = info.getMcId();
			List<UserInfo> userList = op.getNewAddressList(groupMcId);
//			info.setUserList(userList);
			groupList.add(info);
		}
		
//		List<UserInfo> list = new Vector<UserInfo>();
//		Cursor cursor = data;
		Log.d(TAG, "onLoadFinished 1");
		Log.i(TAG,"onLoadFinished groupList = " + groupList);
//		for(Iterator<GroupInfo> itor = groupList.iterator();itor.hasNext();)
//		{
//			
//			Log.i(TAG,"onLoadFinished cursor.getColumnNames() = " + cursor.getCount());
//		}
//		while (cursor.moveToNext()) 
//		{  
//			Log.d(TAG, "onLoadFinished next");
//			UserInfo userInfo = new UserInfo(cursor);
//			list.add(userInfo);
//		}
		Log.d(TAG, "onLoadFinished 2");
		
		cursor.close();
		cursor = null;
		Log.i(TAG,"onLoadFinished end");
		if(callback != null)
		{
			this.callback.onLoadFinished(groupList);
		}
	}
	
	public String getId(Cursor c){
		
		ContentValues args = new ContentValues();
		DatabaseUtils.cursorRowToContentValues(c, args);
		
		String id = args.getAsString(DbConst.COL_Id);
		
		Log.d("lq", "userType = "+args.getAsString(DbConst.COL_Type));
		Log.d("lq", "name = "+args.getAsString(DbConst.COL_Name));
		Log.d("lq", "mcId = "+args.getAsString(DbConst.COL_Id));
		Log.d("lq", "online state = "+args.getAsString(DbConst.COL_State));
		
		//createFromContentValue(args);
		return id;
	}

	@Override
	public void onLoaderReset(Loader<Cursor> loader) {
		// TODO Auto-generated method stub
		this.callback.onLoaderReset();
	}

}
